package de.tum.in.far.threedui.superbrickball.gui;

import javax.media.j3d.Appearance;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import de.tum.in.far.threedui.general.CoordinatesMarker;
import de.tum.in.far.threedui.general.TransformableObject;
import de.tum.in.far.threedui.superbrickball.engine.physics.models.PlaneModel;


public class PlaneObject extends TransformableObject {
	
	Appearance app;
	public PlaneModel planeModel;
	
	int sidePoints = 7;
	float step = .015f;
	SphereObject [][] points = new SphereObject[sidePoints*2+1][sidePoints*2+1];
	
	public PlaneObject(float radius, Appearance app) {
		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
		
		this.app = app;
		transGroup.addChild(new CoordinatesMarker());
		planeModel = new PlaneModel(new Point3f(0,0,1), new Point3f(0.01f,.02f,.00f));
		createPlane();
	}
	

	public PlaneObject(PlaneModel planeModel, Appearance app) {
		this.planeModel = planeModel;
		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
		
		this.app = app;
		transGroup.addChild(new CoordinatesMarker());
		createPlane();
	}

	void createPlane()
	{
		for(int i = - sidePoints ; i <= sidePoints; i ++)
		{
			for(int j = -sidePoints; j <= sidePoints; j ++)
			{
				SphereObject so = new SphereObject(0.001f, app);
				Transform3D soT3d = new Transform3D();
				soT3d.setIdentity();
				Vector3f transVector = new Vector3f(i * step, j * step, planeModel.getZ(i*step, j*step));
//				System.out.println(transVector);
				soT3d.setTranslation(transVector);
				points[sidePoints+i][sidePoints+j] = so;
				so.getTransformGroup().setTransform(soT3d);
				
				transGroup.addChild(so);
			}
		}
		
	}
	
	
	public void setPlaneModel(PlaneModel pl)
	{
		this.planeModel = pl;
		
		for(int i = - sidePoints ; i <= sidePoints; i ++)
		{
			for(int j = -sidePoints; j <= sidePoints; j ++)
			{
				Vector3f transVector = new Vector3f(i * step, j * step, planeModel.getZ(i*step, j*step));
				Transform3D t3d = new Transform3D();
				t3d.setTranslation(transVector);
//				System.out.println(transVector);
				points[sidePoints+i][sidePoints+j].getTransformGroup().setTransform(t3d);
				
			}
		}
	}
}
